package com.k.demo.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.k.demo.entity.Restaurant;
import com.k.demo.service.RestaurantService;

@CrossOrigin
@RestController
@RequestMapping("/restaurant")
public class RestaurantController {
	
	@Autowired
	private RestaurantService restaurantService;
	@Value("${lc.upload.path}")
	private String uploadPath;
	
	@RequestMapping("/upload")
	public Object upload(@RequestParam MultipartFile[] mpfs,HttpServletRequest request) {
		
		
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		String saveFileName = UUID.randomUUID().toString().replaceAll("-", "") + "_" + mpfs[0].getOriginalFilename();
		String filePath = uploadPath+ "\\" + saveFileName;
		System.out.println(filePath);

		File f = new File(filePath);
		
			
		try {
			mpfs[0].transferTo(f);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(f.exists());
		
		map.put("picName",saveFileName);
		
		return map;
	}
	
	@RequestMapping("/update")
	public Object updateMsg(@RequestBody Restaurant restaurant) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("updateState", restaurantService.updateRestaurant(restaurant));
		
		return map;
	}
	
	@RequestMapping("/get")
	public Object getMsg() {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("restaurant", restaurantService.getRestaurant());
		
		return map;
	}
}
